var SpotMaterialModel = function (material) {
  BaseModel.call(this);
  var self = this;

  var imgFields = ['img', 'imgTwo', 'imgThree', 'imgFour'];
  var formFields = ['materialCode', 'materialName', 'model', 'providerName', 'color', 'colorDescription', 'materialType', 'status', 'isStock',
    'unit', 'unitPrice', 'defaultWarehouse', 'safetyStockMin', 'safetyStockMax', 'procurementCycles', 'transmittance', 'material', 'count',
    'creater', 'creationTime', 'lastModifier', 'lastModifyTime', 'purchaseApproval', 'providerNo'
  ];

  var fn = {
    initData: function (material) {
      if (material === undefined) {
        this.initObjectData();
        return;
      }

      self.id = material.id;
      self.setDataFromObj(material, formFields);

      var materialDeptId = material.materialDeptId;
      var materialDeptName = material.materialDeptName;
      if (materialDeptId !== '') {
        self.materialDept = {
          materialDeptId: materialDeptId,
          materialDeptName: materialDeptName
        };
      } else {
        self.materialDept = '';
      }

      var defaultWarehouse = material.defaultWarehouse;
      defaultWarehouse = i18n.getKeyByValue('spotMaterial.defaultWarehouse',defaultWarehouse);
      self.defaultWarehouse = defaultWarehouse;

      var imgUrls = [];
      for (var i = 0; i < imgFields.length; i++) {
        var imgField = imgFields[i];
        var imgUrl = material[imgField];
        if (!SysUtil.isEmpty(imgUrl)) {
          imgUrls.push(imgUrl);
        }
      }
      self.imgUrls = imgUrls;
    },

    initObjectData: function () {
      self.initObject(formFields);
      self.isStock = 1;
      self.unitPrice = undefined;
      self.safetyStockMax = undefined;
      self.safetyStockMin = undefined;
      self.procurementCycles = undefined;
      self.status = "使用";
      self.defaultWarehouse = "1";
      self.imgUrls = [];
      self.materialDept = "";
    }
  };

  this.checkValid = function () {
    var requiredFields = ['materialName', 'model', 'providerName', 'unit'];
    var code = CheckVaildUtil.checkNullField(requiredFields, this);
    if (code !== '') {
      return code;
    }

    var safetyStockMax = this.safetyStockMax,
      safetyStockMin = this.safetyStockMin;
    if (safetyStockMax !== undefined && safetyStockMin !== undefined) {
      if (safetyStockMax < safetyStockMin) {
        return 'stock';
      }
    }

    return '';
  };

  this.getObject = function (type) {
    var postData = {
      isSpot: 1
    };
    if (type === 'update') {
      postData.id = this.id;
    }
    this.setPostData(postData, formFields, true, true);

    var defaultWarehouse = postData.defaultWarehouse;
    postData.defaultWarehouse = i18n.getMsg(`spotMaterial.defaultWarehouse.${defaultWarehouse}`);

    postData.isEquals = this.unit === '付' ? 1 : 0;

    var materialDept = this.materialDept;
    if (!SysUtil.isEmpty(materialDept)) {
      postData.materialDeptId = materialDept.materialDeptId;
    }

    var imgUrls = this.imgUrls;
    for (var i = 0; i < imgFields.length; i++) {
      var imgField = imgFields[i];
      var imgUrl = imgUrls[i];
      if (SysUtil.isEmpty(imgUrl)) {
        imgUrl = '';
      }
      postData[imgField] = imgUrl;
    }
    return postData;
  };

  fn.initData(material);
};
